package com.foreverwin.mes.lims.job;


import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.lims.enums.InspectType;
import com.foreverwin.mes.lims.mapper.InspectPlanDetailMapper;
import com.foreverwin.mes.lims.model.InspectPlan;
import com.foreverwin.mes.lims.model.InspectPlanDetail;
import com.foreverwin.mes.lims.service.InspectTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Configurable
@EnableScheduling
public class InspectTaskJob {

    private static Logger logger = LoggerFactory.getLogger(InspectTaskJob.class);

    @Autowired
    private InspectPlanDetailMapper inspectPlanDetailMapper;
    @Autowired
    private InspectTaskService inspectTaskService;

    /**
     * 按固定时间生成任务
     *    品质巡检
     *    生产巡检
     *    出品检验
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void execute(){
        logger.info("|---->>>自动创建品质巡检任务开始");
        //String site = CommonMethods.getSite();
        String site = "WX01"; //改用quartz可以自动获取。
        List<InspectPlanDetail> planDetails=new ArrayList<>();
        try{
         //查找检验计划
         List<InspectPlanDetail> QIDetails = inspectPlanDetailMapper.selectDetailByType( InspectType.QI.getCode() , site ) ;
         List<InspectPlanDetail> PLDetails = inspectPlanDetailMapper.selectDetailByType( InspectType.PD.getCode() , site ) ;
         List<InspectPlanDetail> PIDetails = inspectPlanDetailMapper.selectDetailByType( InspectType.PI.getCode() , site ) ;
         planDetails.addAll( QIDetails );
         planDetails.addAll( PLDetails );
         planDetails.addAll( PIDetails );

         if ( planDetails.isEmpty() ){
             logger.info("|---->>>自动创建品质巡检任务异常:无计划可以创建任务!"  );
             return;
         }
         inspectTaskService.autoCreatedTask( planDetails , site , "AUTO" );
        }catch ( Exception e ){
            logger.info("|---->>>自动创建品质巡检任务异常:" + e.getMessage() );
        }
        logger.info("|---->>>自动创建品质巡检任务结束"  );
    }

//    @Override
//    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
//            Runnable task = new Runnable() {
//                @Override
//                public void run() {
//
//                }
//            };
//            scheduledTaskRegistrar.addFixedDelayTask(new Runnable() {
//                @Override
//                public void run() {
//                    logger.info("|---->>>自动创建品质巡检任务开始");
//                    //String site = CommonMethods.getSite();
//                    String site = "WX01"; //改用quartz可以自动获取。
//                    List<InspectPlanDetail> planDetails=new ArrayList<>();
//                    try{
//                        //查找检验计划
//                        List<InspectPlanDetail> QIDetails = inspectPlanDetailMapper.selectDetailByType( InspectType.QI.getCode() , site ) ;
//                        List<InspectPlanDetail> PLDetails = inspectPlanDetailMapper.selectDetailByType( InspectType.PD.getCode() , site ) ;
//                        List<InspectPlanDetail> PIDetails = inspectPlanDetailMapper.selectDetailByType( InspectType.PI.getCode() , site ) ;
//                        planDetails.addAll( QIDetails );
//                        planDetails.addAll( PLDetails );
//                        planDetails.addAll( PIDetails );
//
//                        if ( planDetails.isEmpty() ){
//                            logger.info("|---->>>自动创建品质巡检任务异常:无计划可以创建任务!"  );
//                            return;
//                        }
//                        inspectTaskService.autoCreatedTask( planDetails , site , "AUTO" );
//                    }catch ( Exception e ){
//                        logger.info("|---->>>自动创建品质巡检任务异常:" + e.getMessage() );
//                    }
//                    logger.info("|---->>>自动创建品质巡检任务结束"  );
//                }
//            },2000);
//    }
}
